app.controller('BusVehicleModalCtrl', function ($scope,$filter, $modalInstance, $modal, toaster, param, ajax, fileUpload, cos) {

    var inserter = new Req.POST({ resource: $scope.resource });
    var updater = new Req.PUT({ resource: $scope.resource });

    $scope.param = param;
    if ($scope.param.data == null) {
        $scope.param.data = {};
    }

    $scope.timeRange={};

    if ($scope.param.type == 'update') {
        ajax.call(new Req.GET({
            resource: 'vehicle/bs/insurance/' + $scope.param.data.vehicleInsuranceId + '/id'
        })).then(function (result) {
            $scope.insurance = result.result;
            $scope.code = result.code;
        }, function () { });

/*        if($scope.param.data.license!=null){
            $scope.flag = true;
        }
        if($scope.param.data.licenseStart&&$scope.param.data.licenseEnd){
            $scope.timeRange.startTime=$filter("date")($scope.param.data.licenseStart, "yyyy-MM-dd");
            $scope.timeRange.endTime=$filter("date")($scope.param.data.licenseEnd, "yyyy-MM-dd");
        }*/
    }

    //获取组织列表
    $scope.loadOrgList = function () {
        var promise = ajax.call(new Req.GET({
            resource: 'organization/3/list'
        }));
        promise.then(function (result) {
            $scope.orglist = result.result;
            //设置默认值
            if ($scope.param.data.org != null) {
                var id = $scope.param.data.org.id;
                var sel = 0;
                for (var i = 0; i < $scope.orglist.length; i++) {
                    sel++;
                    if ($scope.orglist[i].id == id) {
                        break;
                    }
                }

                $scope.orglist.selected = $scope.orglist[sel - 1];
            } else {
                $scope.orglist.selected = $scope.orglist[0];
            }

        }, function () { });
    };
    $scope.loadOrgList();

    //获取车辆型号配置
    $scope.loadmodel=function(){
        var promise = ajax.call(new Req.GET({
            resource: 'vehicle/bs/model/list'
        }));
        promise.then(function(result) {
            $scope.modelList = result.result;
            //设置默认值
            if ($scope.param.data.modelId != null) {
                var id = $scope.param.data.modelId;
                var sel = 0;
                for (var i = 0; i < $scope.modelList.length; i++) {
                    sel++;
                    if ($scope.modelList[i].id == id) {
                        break;
                    }
                }

                $scope.modelList.selected = $scope.modelList[sel - 1];
            }

        }, function() { });
    }

    $scope.loadmodel();

	//车辆状态
	$scope.loaddplist = function () {
		$scope.dplist =
			[
				{ id: 0, name: "未启用" },
				{ id: 1, name: "正常" },
				{ id: 2, name: "暂停使用" }
			];
		//设置默认值
		if ($scope.param.data.status !=0) {
			var id = $scope.param.data.status;
			$scope.dplist.selected = $scope.dplist[id];
		} else {
			$scope.dplist.selected = $scope.dplist[0];
		}
	};

	$scope.loaddplist();


	$scope.$watch('modelList.selected', function (newVal, oldVal) {
		if (newVal&&$scope.param.type == 'insert') {
            $scope.param.data.model=newVal.model;
            $scope.param.data.seats=newVal.seats;
            $scope.param.data.tickets=newVal.seats - newVal.driverSeats;
		}
	});

	//添加车辆型号
	$scope.addModel=function(){
		$scope.type = 'insert';
        var modalInstance = $modal.open({
            templateUrl: 'template/bus/vehicle/addVehicleModel.html?v=79ca72ad04',
            controller: 'BusModalCtrl',
            size: 'lg',
            resolve: {
                param: function() {
                    return {
                        "type": $scope.type
                    };
                }
            }
        });

        modalInstance.result.then(function(param) {
            if (param.type == 'insert') {
                inserter.fields = param.data;
                inserter.resource = 'vehicle/bs/model/insert';
                ajax.call(inserter).then(function (result) {
                    if (result.code == 200) {
                        $scope.param.data.modelId = result.result.id;
                        $scope.loadmodel();
                        toaster.pop('success', '成功', '新增车辆型号成功。');
                    } else {
                        toaster.pop('error', '错误', result.result);
                    }
                }, function () {
                    toaster.pop('error', '错误', '新增车辆型号失败。');
                });

            }
        });


	}

    //添加保险
    $scope.add = function () {
        $scope.type = 'insert';
        var modalInstance = $modal.open({
            templateUrl: 'template/bus/vehicle/addInsurance.html?v=3e8de1410b',
            controller: 'BusVehicleInsuranceModalCtrl',
            size: 'lg',
            resolve: {
                param: function () {
                    return {
                        "type": $scope.type
                    };
                }
            }
        });

        modalInstance.result.then(function (param) {
            inserter.fields = param.data;
            inserter.resource = 'vehicle/bs/insurance/insert';
            ajax.call(inserter).then(function (result) {
                if (result.code == 200) {
                    $scope.insurance = result.result;
                    $scope.param.data.vehicleInsuranceId=$scope.insurance.id;
                    toaster.pop('success', '成功', '新增保险成功。');
                } else {
                    toaster.pop('error', '错误', result.result);
                }
            }, function () {
                toaster.pop('error', '错误', '新增保险失败。');
            });
        });
    };

    //修改保险
    $scope.update = function () {
        $scope.type = 'update';
        $scope.current_data = angular.copy($scope.insurance);
        var modalInstance = $modal.open({
            templateUrl: 'template/bus/vehicle/addInsurance.html?v=3e8de1410b',
            controller: 'BusVehicleInsuranceModalCtrl',
            size: 'lg',
            resolve: {
                param: function () {
                    return {
                        "data": $scope.current_data,
                        "type": $scope.type
                    };
                }
            }
        });

        modalInstance.result.then(function (param) {
            updater.fields = param.data;
            updater.id = param.data.id;
            updater.resource = 'vehicle/bs/insurance/update';
            ajax.call(updater).then(function (result) {
                if (result.code == 200) {
                    $scope.insurance = result.result;
                    toaster.pop('success', '成功', '修改保险成功。');
                } else {
                    toaster.pop('error', '错误', '修改保险失败！');
                }
            }, function () {
                toaster.pop('error', '错误', '修改保险失败！');
            });
        });
    };



    $scope.ok = function () {
//		var uploadUrl = cos.host() + cos.img() + "?attributeName=license";
		if ($scope.param.type == "insert") {
			if ($scope.myForm.$valid) {
/*				if($scope.insurance == undefined){
	                toaster.pop('warning', '提示', '请添加一个保险！');
	                return;
				}*/
				if($scope.dplist.selected == undefined){
	                toaster.pop('warning', '提示', '请选择车辆状态！');
	                return;
				}
				if($scope.modelList.selected == undefined){
	                toaster.pop('warning', '提示', '请选择车辆型号！');
	                return;
				}
				if($scope.orglist.selected == undefined){
	                toaster.pop('warning', '提示', '请选择所属单位！');
	                return;
				}
/*				if ($scope.timeRange.startTime || $scope.timeRange.endTime) {
	                toaster.pop('warning', '提示', '请选择驾驶证有效时间！');
	                return;
				}
				if($scope.timeRange.startTime||$scope.timeRange.endTime){
					if($scope.timeRange.startTime&&$scope.timeRange.endTime){
						$scope.param.data.licenseStart = $filter("date")($scope.timeRange.startTime, "yyyy-MM-dd");
						$scope.param.data.licenseEnd =$filter("date")($scope.timeRange.endTime, "yyyy-MM-dd");
					}else{
		                toaster.pop('warning', '提示', '请填写驾驶证起止时间！');
		                return;
					}
				}*/
				if($scope.insurance== undefined){
                    $scope.param.data.vehicleInsuranceId=0;
                }
				$scope.param.data.status=$scope.dplist.selected.id;
				$scope.param.data.modelId=$scope.modelList.selected.id;
				$scope.param.data.org=$scope.orglist.selected;
				$modalInstance.close($scope.param);

/*				if($scope.fileToUpload != undefined){
					fileUpload.upload($scope.fileToUpload, uploadUrl, null,
							function (result) {
								if (result.code == 200) {
									$scope.param.data.licensePhoto = result.filePath;
									$modalInstance.close($scope.param);
								} else {
									toaster.pop('error', '错误', result.message);
									return ;
								}
							}, function () {
								toaster.pop('error', '错误', '文件上传失败！');
							}
						);
				}else{
					$modalInstance.close($scope.param);
				}*/
			}
		} else if ($scope.param.type == "update") {
			if ($scope.myForm.$valid) {
/*				if($scope.timeRange.startTime||$scope.timeRange.endTime){
					if($scope.timeRange.startTime&&$scope.timeRange.endTime){
						$scope.param.data.licenseStart = $filter("date")($scope.timeRange.startTime, "yyyy-MM-dd");
						$scope.param.data.licenseEnd =$filter("date")($scope.timeRange.endTime, "yyyy-MM-dd");
					}else{
		                toaster.pop('warning', '提示', '请填写驾驶证起止时间！');
		                return;
					}
				}*/
				$scope.param.data.status=$scope.dplist.selected.id;
				$scope.param.data.modelId=$scope.modelList.selected.id;
				$scope.param.data.org=$scope.orglist.selected;
				$modalInstance.close($scope.param);
/*				if($scope.fileToUpload != undefined){
					fileUpload.upload($scope.fileToUpload, uploadUrl, null,
							function (result) {
								if (result.code == 200) {
									$scope.param.data.licensePhoto = result.filePath;
									$modalInstance.close($scope.param);
								} else {
									toaster.pop('error', '错误', result.message);
									return ;
								}
							}, function () {
								toaster.pop('error', '错误', '文件上传失败！');
							}
						);
				}else{
					$modalInstance.close($scope.param);
				}*/

			}
		}
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});